/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HMSERIALIZATION_YYJSON_UTIL_H
#define HMSERIALIZATION_YYJSON_UTIL_H

#include <memory>
#include <string>
#include "json_node.h"
#include "yyjson.h"
#include "napi/native_api.h"

#if defined(__GNUC__) || defined(__clang__)
#define LIKELY(x)   __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define LIKELY(x)   (x)
#define UNLIKELY(x) (x)
#endif

typedef enum YYJSONToNapiMode {
    DEFAULT = 0,
    JSONNODE = 1,
} YYJSONToNapiMode;

namespace yyjson_util {
struct yyjson_doc_deleter {
    void operator()(yyjson_doc *doc) const
    {
        if (doc) {
            yyjson_doc_free(doc);
        }
    }
};

struct yyjson_incr_state_deleter {
    void operator()(yyjson_incr_state *state) const
    {
        if (state) {
            yyjson_incr_free(state);
        }
    }
};

struct yyjson_mut_doc_deleter {
    void operator()(yyjson_mut_doc *mdoc) const
    {
        if (mdoc) {
            yyjson_mut_doc_free(mdoc);
        }
    }
};

struct FreeDeleter {
    void operator()(char* p) const noexcept
    {
        free(p);
    }
};

struct CharPtrDeleter {
    void operator()(char* p) const
    {
        delete[] p;
    }
};

struct AsyncData {
    JsonNode *obj;
    napi_deferred deferred;
    char *data;
    size_t byteLength;
    uint32_t flag;
    yyjson_doc* doc;
    bool success;
    std::string errorMsg;
};

using unique_yyjson_doc = std::unique_ptr<yyjson_doc, yyjson_doc_deleter>;

using unique_yyjson_incr_state = std::unique_ptr<yyjson_incr_state, yyjson_incr_state_deleter>;

using unique_yyjson_mut_doc = std::unique_ptr<yyjson_mut_doc, yyjson_mut_doc_deleter>;

using unique_char_ptr = std::unique_ptr<char[], CharPtrDeleter>;

// 长生命周期的 bool 类型 napi_value
inline napi_value g_napiTrue;
inline napi_value g_napiFalse;

constexpr int64_t numberMinValue = -9007199254740992LL; // -2^53
constexpr int64_t numberMaxValue = 9007199254740992LL; // 2^53

/**
 * Convert a yyjson value into a Node-API value.
 * @param env
 * @param val
 * @param mode
 * @return
 */
napi_value YYJSONToNapi(napi_env env, yyjson_val *val, size_t mode);

/**
 * Convert a Node-API value into a mutable yyjson value.
 * @param env
 * @param doc
 * @param value
 * @return
 */
yyjson_mut_val* NapiToYYJSON(napi_env env, yyjson_mut_doc* doc, napi_value value);

/**
 * Get the block size for streaming parsing.
 * @param byteLength
 * @return
 */
size_t GetBlockSize(size_t byteLength);

/**
 * Asynchronously performs incremental JSON parsing (Incremental JSON Parsing).
 * @param env
 * @param data
 */
void ParseIncAsync(napi_env env, void* data);

/**
 * Completion callback for asynchronous incremental JSON parsing.
 * @param env
 * @param status
 * @param data
 */
void ParseIncAsyncCompleted(napi_env env, napi_status status, void* data);

/**
 * Completion callback for asynchronous JSON node parsing.
 * @param env
 * @param status
 * @param data
 */
void ParseJsonNodeAsyncCompleted(napi_env env, napi_status status, void* data);

/**
 * get default yyjson read flag
 * @return
 */
yyjson_read_flag GetDefaultReadFlag();

/**
 * get default yyjson write flag
 * @return
 */
yyjson_write_flag GetDefaultWriteFlag();
}

#endif // HMSERIALIZATION_YYJSON_UTIL_H
